package blackitheima.Lock;

import java.util.concurrent.locks.ReentrantLock;

//卖票
public class MyRounnable implements Runnable {
    //票数
    private int tickCount=100;
    //创建锁对象  true:表示公平
    ReentrantLock lock=new ReentrantLock(true);
    @Override
    public void run() {
        while(true){
            lock.lock();
            try {
                if(tickCount==0){
                    break;
                }else{
                    Thread.sleep(100);
                    tickCount--;
                    System.out.println(Thread.currentThread().getName()+"在卖票,还剩"+tickCount+"张票");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

    }
}
